package com.cqs.example.io.serial.utils;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.HessianFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class HessianSerializer {
    private static final HessianFactory hf = new HessianFactory();

    public static void write(Object o, OutputStream outputStream){
        Hessian2Output output = hf.createHessian2Output(outputStream);
        try {
            output.writeObject(o);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static <T> T read(InputStream inputStream) {
        Hessian2Input input = hf.createHessian2Input(inputStream);
        try {
            return (T)input.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
